<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <link href="https://cdn.bootcss.com/zTree.v3/3.5.29/css/zTreeStyle/zTreeStyle.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/zTree.v3/3.5.29/js/jquery.ztree.all.min.js"></script>

</head>
<body>

<div id="ztree"></div>
<script>
    var setting = {
        edit: {
            enable: true,   // 启用编辑功能
            editNameSelectAll: true,    // 编辑状态下是否允许全选
            showRemoveBtn: true,    // 显示删除按钮
            showRenameBtn: true     // 显示重命名按钮
        },
        data: {
            simpleData: {
                enable: true   // 启用简单数据格式
            }
        }
    };

    var zNodes = [
        {id:1, pId:0, name:"父节点 1", open:true},
        {id:11, pId:1, name:"子节点 1-1"},
        {id:12, pId:1, name:"子节点 1-2"},
        {id:13, pId:1, name:"子节点 1-3"},
        {id:2, pId:0, name:"父节点 2", open:true},
        {id:21, pId:2, name:"子节点 2-1"},
        {id:22, pId:2, name:"子节点 2-2"},
        {id:23, pId:2, name:"子节点 2-3"},
        {id:3, pId:0, name:"父节点 3", open:true},
        {id:31, pId:3, name:"子节点 3-1"},
        {id:32, pId:3, name:"子节点 3-2"},
        {id:33, pId:3, name:"子节点 3-3"}
    ];

    $(document).ready(function(){
        $.fn.zTree.init($("#ztree"), setting, zNodes);
    });

    var setting = {
        edit: {
            enable: true,
            editNameSelectAll: true,
            showRemoveBtn: true,
            showRenameBtn: true,
            drag: {
                autoExpandTrigger: true,
                prev: dropPrev,
                inner: dropInner,
                next: dropNext
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            beforeDrag: beforeDrag,
            beforeDrop: beforeDrop,
            onDrop: onDrop
        }
    };

    function beforeDrag(treeId, treeNodes) {
// 判断是否为根节点
        for (var i=0, l=treeNodes.length; i<l; i++) {
            if (treeNodes[i].level == 0) {
                return false;
            }
        }
        return true;
    }

    function beforeDrop(treeId, treeNodes, targetNode, moveType) {
// 判断是否为根节点或父节点是targetNode
        for (var i=0, l=treeNodes.length; i<l; i++) {
            if (treeNodes[i].level == 0 || targetNode && treeNodes[i].id == targetNode.id || targetNode && targetNode.getParentNode() && treeNodes[i].id == targetNode.getParentNode().id) {
                return false;
            }
        }
        return true;
    }

    function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
// 添加新节点
        if (moveType == "inner") {
            var newNode = {id: new Date().getTime(), pId: targetNode.id, name: "新节点"};
            $.fn.zTree.getZTreeObj(treeId).addNodes(targetNode, newNode);
        } else {
            var newNode = {id: new Date().getTime(), pId: targetNode.getParentNode().id, name: "新节点"};
            $.fn.zTree.getZTreeObj(treeId).addNodes(targetNode.getParentNode(), newNode);
        }
    }

</script>
</body>
</html>

